#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIApplication.h>
#import <UIKit/UIPushButton.h>
#import <UIKit/UIThreePartButton.h>
#import <UIKit/UINavigationBar.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UITable.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UITableColumn.h>
#import <UIKit/UINavBarButton.h>
#import <UIKit/UIGradientBar.h>
#import <UIKit/UISegmentedControl.h>
#import "CDList.h"
#import "SetupView.h"
#import "iCountDown.h"

#include <stdlib.h>
#include <stdio.h>
#include <objc/objc.h>
#include <objc/objc-runtime.h>

// This method was added to remove a linker error found in V04.dmg toolchain
double objc_msgSend_fpret(id self, SEL op, ...) 
{
   Method method = class_getInstanceMethod(self->isa, op);
   int numArgs = method_getNumberOfArguments(method);

   if(numArgs == 2) {
        double (*imp)(id, SEL);
        imp = (double (*)(id, SEL))method->method_imp;
        return imp(self, op);
    } else if(numArgs == 3) {
         va_list ap;
         va_start(ap, op);
         double (*imp)(id, SEL, void *);
         imp = (double (*)(id, SEL, void *))method->method_imp;
         return imp(self, op, va_arg(ap, void *));
    }

    fprintf(stderr, 
       "ERROR: objc_msgSend_fpret called on <%s %p> with selector %s had to return 0.0\n", object_getClassName(self), 
        self, sel_getName(op));
    return 0.0;
}

@implementation iCountDown

- (void) initApplication
{
  //setup up the application code location on google 	
  _applicationID = @"icountdown.googlecode.com";
  
  //NSLog(@"iCountDown: initApplication\n");
  
  //set the internal version for the application
  _version = @"2";
	
  //ask the iphone how big its screen is to be used for the app	
  rect = [UIHardware fullScreenApplicationContentRect];
  rect.origin.x = rect.origin.y = 0.0f;
	
  // Set up window and main View
  _window = [[UIWindow alloc] initWithContentRect: rect];
  _mainView = [[UIView alloc] initWithFrame: rect];

  //initialize the display window
  [_window orderFront: self];
  [_window makeKey: self];
  [_window _setHidden: NO];
  [_window setContentView: _mainView];
	
  // set up count down list view
  _cdlist = [[CDList alloc] initWithApplication: self
		withAppID: _applicationID
		withFrame: CGRectMake(0.0f, 0.0f, rect.size.width, rect.size.height)];
  [_cdlist setDelegate: self];
	
  // set up setup view for preferences
  _setupView = [[SetupView alloc] initWithApplication: self
		withAppID: _applicationID
		withFrame: CGRectMake(0.0f, 0.0f, rect.size.width, rect.size.height)
		withCDList: _cdlist];
  [_setupView setDelegate: self];
  [_cdlist setSetupAccess: _setupView];
	
  // activate the count down table view
  [self makeCDListActive];
  
}

//retrieve the application version
- (NSString*) getVersion
{
	return _version;
}

// display the count down table.
- (void) makeCDListActive
{
	[_cdlist displayTable];
	[_mainView addSubview: _cdlist];	
}

- (void) applicationDidFinishLaunching: (id) unused
{
    // Init the application and setup the twp main view
	[self initApplication];
}

- (void) applicationWillTerminate
{
}

@end